//
// File:	BaseGameRule.cpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Created:	23/04/2010 at 23:08:13
// Purpose:	
//
#include "Base.hpp"
#include "BaseGameRule.hpp"
//---TEMP---
#include "TestEntities.hpp"
//---
#include "Trace.hpp"

float BaseGameRule::stScreenScrollSpeed = 40.0f;

//------------------------------------------------------------------
// Constructor / Destructor
//------------------------------------------------------------------
BaseGameRule::BaseGameRule()
{
}

BaseGameRule::~BaseGameRule()
{
}

//------------------------------------------------------------------
// Start / Stop
//------------------------------------------------------------------
bool BaseGameRule::OnStart()
{
	//Spawn the player(s)
	tEntList spawnpointsList = GlobalVariables::EntityManager::Get().GetEntitiesByClassName("info_player_start");
	if (spawnpointsList.empty())
	{
		Trace::warning << "BaseGameRule::OnStart: The level has no info_player_start" << std::endl;
		return false;
	}
	BaseEntity *pSpawnPoint = dynamic_cast<BaseEntity *>(spawnpointsList.back());					//TEMP
	BasePlayer *pPlayer = dynamic_cast<BasePlayer *>(BaseEntity::CreateEntityByName("player"));		//TEMP
	pPlayer->SetX(pSpawnPoint->GetX());																//TEMP
	pPlayer->SetY(pSpawnPoint->GetY());																//TEMP
	pPlayer->SetColorName(BasePlayer::Green);														//TEMP

	//Setup the view scroll entity(ies) speed
	tEntList scrollentsList = GlobalVariables::EntityManager::Get().GetEntitiesByClassName("info_scroll_entity");
	if (scrollentsList.empty())
	{
		Trace::warning << "BaseGameRule::OnStart: The level has no info_scroll_entity" << std::endl;
		return false;
	}
	tEntList::iterator it = scrollentsList.begin();
	tEntList::iterator ite = scrollentsList.end();
	for (; it != ite; ++it)
	{
		(*it)->SetVelocityX(BaseGameRule::stScreenScrollSpeed);
		(*it)->SetVelocityY(0.0f);
	}
	return true;
}

void BaseGameRule::OnStop()
{
}

//------------------------------------------------------------------
// Update
//------------------------------------------------------------------
void BaseGameRule::Update()
{
	//Automatically move the player(s) if they are not moving
	tEntList playersList = GlobalVariables::EntityManager::Get().GetEntitiesByClassName("player");
	tEntList::iterator it = playersList.begin();
	tEntList::iterator ite = playersList.end();
	for (; it != ite; ++it)
	{
		if ((*it)->GetVelocityX() == 0.0f)
			(*it)->SetVelocityX(BaseGameRule::stScreenScrollSpeed);
	}
}

//------------------------------------------------------------------
// Create
//------------------------------------------------------------------
IGameRule *BaseGameRule::Create()
{
	return new BaseGameRule();
}
